package com.dzj.fishingcommunity.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dzj.fishingcommunity.mapper.LikeRecordMapper;
import com.dzj.fishingcommunity.model.LikeRecord;
import com.dzj.fishingcommunity.service.LikeRecordService;
import org.springframework.stereotype.Service;

@Service
public class LikeRecordServiceImpl extends ServiceImpl<LikeRecordMapper, LikeRecord> implements LikeRecordService {

    /**
     * 根据帖子id给点赞记录表加1
     * @param postId
     * @return
     */
    @Override
    public Boolean addCountByPostIdAndUserId(Integer postId) {

        String sql = "total_likes = total_likes + 1";

        //根据帖子id给点赞记录表加1
        boolean update = lambdaUpdate()
                .eq(LikeRecord::getPostId, postId)
                .setSql(sql)
                .update();

        return update;
    }

    /**
     * 将点赞数-1，取消点赞数加1
     * @param postId
     */
    @Override
    public Boolean cancelLike(Integer postId) {

        String sql = "total_likes = total_likes - 1, cancelled_likes = cancelled_likes + 1";

        //将点赞数-1，取消点赞数加1
        boolean update = lambdaUpdate()
                .eq(LikeRecord::getPostId, postId)
                .setSql(sql)
                .update();
        return update;
    }
}